package com.butuh.uang.tuh.ang.present;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.butuh.uang.tuh.ang.model.BaseResult;
import com.butuh.uang.tuh.ang.model.BeannerResult;
import com.butuh.uang.tuh.ang.model.HomeListBean;
import com.butuh.uang.tuh.ang.model.HomeListResult;
import com.butuh.uang.tuh.ang.model.ProductDetailResult;
import com.butuh.uang.tuh.ang.model.YinSiResult;
import com.butuh.uang.tuh.ang.net.Api;
import com.butuh.uang.tuh.ang.ui.MainActivity;
import com.butuh.uang.tuh.ang.util.I18NUtils;
import com.butuh.uang.tuh.ang.util.JsonUtils;
import com.butuh.uang.tuh.ang.util.RandomUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import okhttp3.RequestBody;

public class PMain extends XPresent<MainActivity> {


    public void getHomeList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("923", "1");
        RequestBody body = JsonUtils.mapToBody(paramsMap);
        Api.getNetService().getList(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<HomeListResult>getApiTransformer())
                .compose(XApi.<HomeListResult>getScheduler())
                .compose(getV().<HomeListResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<HomeListResult>() {
                    @Override
                    public void onNext(HomeListResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                        getV().loadList(stBean.getBody().getDatum());
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                        getV().loadListError(error.getType(), error.getMessage());
                    }
                });
    }

    public void getBanner() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("938", "1");
        paramsMap.put("939", "965,958");
        paramsMap.put("940", "banner");
        RequestBody body = JsonUtils.mapToBody(paramsMap);
        Api.getNetService().getBanner(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<BeannerResult>getApiTransformer())
                .compose(XApi.<BeannerResult>getScheduler())
                .compose(getV().<BeannerResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<BeannerResult>() {
                    @Override
                    public void onNext(BeannerResult stBean) {
                        if (null != stBean.getBody()) {
                            if (null != stBean.getBody().getDatum()) {
                                if (stBean.getBody().getDatum().size() != 0) {
                                    getV().setBannerSuccess(stBean.getBody().getDatum().get(0));
                                } else {
                                    getV().setBannerSuccess(null);
                                }
                            } else {
                                getV().setBannerSuccess(null);
                            }
                        } else {
                            getV().setBannerSuccess(null);
                        }
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                    }
                });
    }


    public void getEasyList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("918", "easy");
        RequestBody body = JsonUtils.mapToBody(paramsMap);
        Api.getNetService().getList(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<HomeListResult>getApiTransformer())
                .compose(XApi.<HomeListResult>getScheduler())
                .compose(getV().<HomeListResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<HomeListResult>() {
                    @Override
                    public void onNext(HomeListResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                        getV().loadEasyList(stBean.getBody().getDatum());
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                        getV().loadListError(error.getType(), error.getMessage());
                    }
                });
    }


    public void getNewList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("918", "new");
        RequestBody body = JsonUtils.mapToBody(paramsMap);
        Api.getNetService().getList(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<HomeListResult>getApiTransformer())
                .compose(XApi.<HomeListResult>getScheduler())
                .compose(getV().<HomeListResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<HomeListResult>() {
                    @Override
                    public void onNext(HomeListResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                        getV().loadNewSuccess(stBean.getBody().getDatum());
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
//                        getV().loadListError(error.getType(), error.getMessage());
                    }
                });
    }


    public void getProductDetail(int id) {
        List<Integer> list = new ArrayList<>();
        list.add(id);
        RequestBody body = JsonUtils.strToBody(list.toString());
        Api.getNetService().getProductDetail(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<ProductDetailResult>getApiTransformer())
                .compose(XApi.<ProductDetailResult>getScheduler())
                .compose(getV().<ProductDetailResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<ProductDetailResult>() {
                    @Override
                    public void onNext(ProductDetailResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                        if (stBean.getBody().size() > 0) {
                            getV().loadDataSuccess(stBean.getBody().get(0));
                        } else {
                            getV().loadDataSuccess(null);
                        }

                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                        getV().loadDataFail(error.getMessage());

                    }
                });
    }


    public void setdeviceinfo(Context context, String referrer, String advertisingId, String token) {
        WindowManager wm = (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

        XLog.e("======token====" + token);
        XLog.e("======adid=====" + advertisingId);
        XLog.e("======referrer=====" + referrer);

        XLog.e("====== metrics.density=====" + metrics.density);
        XLog.e("====== metrics.scaledDensity=====" + metrics.scaledDensity);
        XLog.e("====== metrics.xdpi=====" + metrics.xdpi);
        XLog.e("====== metrics.ydpi=====" + metrics.ydpi);
        XLog.e("====== metrics.widthPixels=====" + metrics.widthPixels);
        XLog.e("====== metrics.heightPixels=====" + metrics.heightPixels);
        XLog.e("====== Build.ID=====" + Build.ID);
        XLog.e("====== Build.MODEL=====" + Build.MODEL);
        XLog.e("====== Build.MANUFACTURER=====" + Build.MANUFACTURER);
        XLog.e("====== Build.PRODUCT=====" + Build.PRODUCT);
        XLog.e("====== Build.BRAND=====" + Build.BRAND);
        XLog.e("====== Build.VERSION.RELEASE=====" + Build.VERSION.RELEASE);
        XLog.e("====== Build.VERSION.SDK_INT=====" + Build.VERSION.SDK_INT);
        XLog.e("====== 系统语言=====" + I18NUtils.getCurrentLanguage(context));
        XLog.e("====== 系统时区=====" + I18NUtils.getCurrentTimeZone());


        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("838", Build.ID);
        paramsMap.put("839", Build.MODEL);
        paramsMap.put("840", Build.MANUFACTURER);
        paramsMap.put("841", Build.BRAND);
        paramsMap.put("842", Build.PRODUCT);
        paramsMap.put("843", Build.VERSION.RELEASE);
        paramsMap.put("845", String.valueOf(Build.VERSION.SDK_INT));
        paramsMap.put("861", String.valueOf(metrics.widthPixels));
        paramsMap.put("862", String.valueOf(metrics.heightPixels));
        paramsMap.put("863", String.valueOf(metrics.density));
        paramsMap.put("864", String.valueOf(metrics.scaledDensity));
        paramsMap.put("865", String.valueOf(metrics.xdpi));
        paramsMap.put("866", String.valueOf(metrics.ydpi));
        paramsMap.put("873", advertisingId);
        paramsMap.put("884", I18NUtils.getCurrentTimeZone());
        paramsMap.put("885", I18NUtils.getCurrentLanguage(context));
        paramsMap.put("982", token);
        paramsMap.put("983", referrer);
        RequestBody body = JsonUtils.mapToBody(paramsMap);
        Api.getNetService().setdeviceinfo(JsonUtils.getAllParmarts(getV()), body)
                .compose(XApi.<BaseResult>getApiTransformer())
                .compose(XApi.<BaseResult>getScheduler())
                .compose(getV().<BaseResult>bindToLifecycle())
                .subscribe(new ApiSubscriber<BaseResult>() {
                    @Override
                    public void onNext(BaseResult stBean) {
                        XLog.e("--token--" + stBean.toString());

                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());

                    }
                });
    }




}
